package com.example.comiccenter.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;

import java.util.Calendar;
import java.util.HashMap;

public class JWTUtil {
    final static String secret = "!34ADAS";

    public static String getToken(String username){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 1);

        return JWT.create()
                .withHeader(new HashMap<>())  // Header
                .withClaim("userName", username)
                .withExpiresAt(calendar.getTime())  // 过期时间
                .sign(Algorithm.HMAC256(secret));
    }

    public static boolean checkToken(String token){
        JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(secret)).build();
        try {
            jwtVerifier.verify(token);
            return true;
        } catch (JWTVerificationException e) {
            return false;
        }
    }
}
